// merchants/provide/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tagindex: 1,//1多选单张2单选多张
    couponList: [],
    useuid: 0,
    backurl: '',
    currentSelect: '',
    currentNumber: 1
  },

  // 我创建的优惠券
  getMycreatCounpons: function() {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Coupon/GetCouponTypePage',
      data: {
        storeId: app.globalData.storeId,
        type: 0,
        couponTypeName: -1,
        createuid: app.globalData.userInfo.Uid,
        pageSize: 50
      },
      method: 'POST',
      success: function(res) {
        let couponList = res.data.Data;
        for (let i = 0; i < couponList.length; i++) {
          couponList[i].sendstarttime = couponList[i].sendstarttime.substr(0, 10);
          couponList[i].sendendtime = couponList[i].sendendtime.substr(0, 10);
          couponList[i].isSelected = 0;
          if (couponList[i].count>0){
            couponList[i].sendNumber = 1;
          }else{
            couponList[i].sendNumber = 0;
          }
        }
        that.setData({
          couponList: couponList
        })
      }
    })
  },
  getCouponTypeListByBetch: function(){
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Coupon/GetCouponTypeListByBetch?createuid=' + app.globalData.userInfo.Uid + '&uid=' + that.data.useuid,
      success: function(res){
        if(res.data.Code==0){
          let couponList = res.data.Data;
          for (let i = 0; i < couponList.length; i++) {
            couponList[i].sendstarttime = couponList[i].sendstarttime.substr(0, 10);
            couponList[i].sendendtime = couponList[i].sendendtime.substr(0, 10);
            couponList[i].isSelected = 0;
            if (couponList[i].count > 0) {
              couponList[i].sendNumber = 1;
            } else {
              couponList[i].sendNumber = 0;
            }
          }
          that.setData({
            couponList: couponList
          })
        }else{
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  // 批量选择要发给客户的优惠券
  selectCoupontypeid: function(e) {
    let selectCoupontypeid = e.currentTarget.dataset.coupontypeid;
    let couponList = this.data.couponList;
    if(this.data.tagindex==1){
      for (let i = 0; i < couponList.length; i++) {
        if (selectCoupontypeid == couponList[i].coupontypeid) {
          if (!couponList[i].isSelected) {
            couponList[i].isSelected = 1;
          } else {
            couponList[i].isSelected = 0;
          }
        }
      }
      this.setData({
        couponList: couponList
      })
    }
    if(this.data.tagindex==2){
      this.setData({
        currentSelect: selectCoupontypeid
      })
    }
  },

  // 发给客户
  provideTocustomer: function() {
    let that = this;
    let cardId = wx.getStorageSync("cardId");
    var selectCoupontypeids = "";
    var couponList = that.data.couponList;
    let appendCount = 1;
    if(that.data.tagindex==1){//多选单张
      for (var i = 0; i < couponList.length; i++) {
        if (couponList[i].isSelected) {
          selectCoupontypeids += couponList[i].coupontypeid + ",";
        }
      }
      if (selectCoupontypeids.length == 0) {
        wx.showModal({
          title: '提示',
          content: "请选择优惠券！",
          showCancel: false,
        })
        return;
      }
      selectCoupontypeids = selectCoupontypeids.substr(0, selectCoupontypeids.length - 1);
    }
    if(that.data.tagindex==2){//单选多张
      selectCoupontypeids = that.data.currentSelect;
      appendCount = that.data.currentNumber;
      if (selectCoupontypeids.length == 0) {
        wx.showModal({
          title: '提示',
          content: "请选择优惠券！",
          showCancel: false,
        })
        return;
      }
    }
    wx.request({
      url: app.globalData.host + '/api/Coupon/BatchReceiveCoupon',
      data: {
        couponTypeIds: selectCoupontypeids,
        uid: that.data.useuid,
        appendCount: appendCount
      },
      method: 'POST',
      success: function(res) {
        if (res.data.Code == 0) {
          wx.showToast({
            title: '发放成功',
            duration: 5000,
            success: function(res) {
              if (that.data.backurl) {
                wx.navigateTo({
                  url: '/pages/h5page/index?url=' + that.data.backurl,
                })
              } else {
                wx.redirectTo({
                  url: '/pages/home/index?id=' + cardId,
                })
              }
            }
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.backurl) {
      this.setData({
        backurl: options.backurl
      })
    }
    if (options.useuid) {
      this.setData({
        useuid: options.useuid
      })
    }
    this.getMycreatCounpons();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  changeTag: function(e){
    let tagindex = e.currentTarget.dataset.tagindex;
    if (tagindex==1){
      this.getMycreatCounpons();
    }
    if(tagindex==2){
      this.getCouponTypeListByBetch();
    }
    this.setData({
      tagindex: tagindex
    })
  },
  // 减
  numJianTap: function (e) {
    let that = this;
    let coupontypeid = e.currentTarget.dataset.coupontypeid;
    let couponList = that.data.couponList;
    for (let i = 0; i < couponList.length; i++) {
      if (couponList[i].coupontypeid == coupontypeid){
        if (couponList[i].sendNumber > 1) {
          let currentNumber = couponList[i].sendNumber--;
          that.setData({
            couponList: couponList,
            currentNumber: currentNumber
          })
        }
        break;
      }
    }
    
  },
  // 加
  numJiaTap: function (e) {
    let that = this;
    let coupontypeid = e.currentTarget.dataset.coupontypeid;
    let couponList = that.data.couponList;
    for (let i = 0; i < couponList.length; i++) {
      if (couponList[i].coupontypeid == coupontypeid) {
        if (couponList[i].sendNumber < couponList[i].count) {
          let currentNumber = couponList[i].sendNumber++;
          that.setData({
            couponList: couponList,
            currentNumber: currentNumber
          })
        }
        break;
      }
    }
  },
  bindnumber: function (e) {
    if(e.detail.value > 100){
      wx.showModal({
        title: '提示',
        content: '单次发放的优惠券不可大于100张',
        showCancel: false
      })
      return;
    }
    let that = this;
    let coupontypeid = e.currentTarget.dataset.coupontypeid;
    let couponList = that.data.couponList;
    for (let i = 0; i < couponList.length; i++) {
      if (couponList[i].coupontypeid == coupontypeid) {
        if (e.detail.value <= couponList[i].count) {
          couponList[i].sendNumber = e.detail.value;
          that.setData({
            couponList: couponList,
            currentNumber: e.detail.value
          })
        }else{
          wx.showModal({
            title: '提示',
            content: '剩余优惠券不足,',
            showCancel: false
          })
        }
        break;
      }
    }
  },


})